package com.xxl.job.feign;

import feign.Feign;
import feign.Logger;
import feign.Request;
import feign.form.FormEncoder;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.okhttp.OkHttpClient;
import feign.slf4j.Slf4jLogger;
import feign.spring.SpringContract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author Tang
 * @classname FeignRpcConfiguration
 * @description [  ]
 * @date 2023/4/5 14:14
 */
@ConditionalOnExpression(value = "'${xxljob.admin_url:null}' != 'null' ")
@Configuration
public class FeignRpcConfiguration {

    @Value(value = "${xxljob.admin_url}")
    public String adminUrl;

    @Bean
    public XxlJobAdminRpc xxlJobAdminRpc() {
        return Feign.builder()
                .client(new OkHttpClient())
                .encoder(new FormEncoder(new GsonEncoder()))
                .decoder(new GsonDecoder())
                .options(new Request.Options(3L, TimeUnit.SECONDS, 3L,TimeUnit.SECONDS,true))
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                .requestInterceptor(new TokenRequestInterceptor())
                .contract(new SpringContract())
        .target(XxlJobAdminRpc.class,adminUrl);
    }

}
